﻿using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using CommonTools;
using shop.Model;

namespace shop.Bll.BasicInfo
{

	/// <summary> 业务员 基础资料 </summary>
	public class JSalerBll : BasicBllParent, IFastQueryBll<JSubMessage>
	{

		/// <summary>
		/// 查询
		/// </summary>
		/// <param name="code">查询代码</param>
		/// <param name="isDetails">是否明细</param>
		/// <param name="msg">错误消息</param>
		/// <returns></returns>
		public override IEnumerable<object> Query(string code, bool isDetails, out string msg)
		{
			msg = null;

			var list=DB.FSql.Select<JSubMessage>()
				.Where(a => a.Isdel == false && a.Code.StartsWith("5."));

			list = list.Where(a => a.IsDetails == isDetails);
			return list.ToList();
		}
		


		#region 实现IFastQueryBll 接口, 在FastBox中使用

		/// <summary> 在查询窗体,报表中显式的名称 </summary>
		public string Title => "销售业务员";

		/// <summary> 本地存储的明细 </summary>
		/// <remarks>如果基础资每次都从数据库查询, 这个没用, 保持null</remarks>
		public List<JSubMessage> Items => null;


		/// <summary> 是否每次从数据库查询 </summary>
		public bool QueryFromDataBase => true;

		
		/// <summary> 存储在快速查询窗体中,显示的属性,及属性的显示, 这个暂时不用,以后删除 </summary>
		public Dictionary<string, string> DisplayColumns => null;


		/// <summary> 显示的列, 静态 </summary>
		private static readonly string[] _columns = new string[] { nameof(JSubMessage.Code), nameof(JSubMessage.Name) };


		/// <summary> 存储在快速查询窗体中,显示的属性 </summary>
		public string[] Columns => _columns;


		/// <summary> 实体模型的代码属性名称 </summary>
		public string CodeField => nameof(JSubMessage.Code);



		/// <summary> 代码中级别的分隔符 </summary>
		public override string Separator => ".";


		public override string LookRight => "01.10.1";

		public override string SaveRight => "01.10.2";

		public override string CheckRight => "01.10.5";


		/// <summary> 查询树形图的所有节点 </summary>
		public override IEnumerable<IFastTree> QueryTreeList()
		{
			var u = new JSubMessage()
			{
				Name = "业务员", Code = "5",
			};

			return new List<IFastTree>()
			{
				u 
			};
		} 

		/// <summary> 查询方法 </summary>
		public List<JSubMessage> Query(string findText, string prefixCode = null)
		{
			return DB.FSql.Select<JSubMessage>().Where(a => a.Isdel ==false && a.Code.StartsWith("5.") 
				&& (a.Code.Contains(findText) || a.Name.Contains(findText) ))
				.ToList();
		}


		/// <summary> 查询树形图的一个节点下的所有内容 </summary>
		public List<JSubMessage> QueryByGroup(string group)
		{
			return Query(group);
		}


		public override bool AddGroup(object obj, IUser user, out string err)
		{
			err = "此功能无效";
			return false;
		}

		public override bool DeleteGroup(object obj, IUser user, out string err)
		{
			err = "此功能无效";
			return false;
		}

		public override bool UpdateGroup(object obj, IUser user, out string err)
		{
			err = "此功能无效";
			return false;
		}

		public override bool Insert(object obj, IUser user, out string err)
		{
			if (!(obj is JSubMessage model ))
			{
				err = "系统错误";
				return false;   
			}

			if (string.IsNullOrEmpty(model.Name))
			{
				err = "名称不能为空!";
				return false;   
			}

			//最大编号
			var num = DB.FSql.Select<JSubMessage>()
				.Where(a => a.Isdel == false && a.Code.StartsWith("5."))
				.Max(a => a.Code);
			int numInt = 0;
			if (string.IsNullOrEmpty(num))
			{
				numInt = 1;
			}
			else
			{
				numInt = num.Substring(2).IsNull(0) + 1;
			}
			num = "5." + (numInt.ToString().PadLeft(3, ' '));   //最大编号

			model.Code = num;

			var id = DB.FSql.Insert(model).ExecuteIdentity();
			model.ID = (int)id;
			err = string.Empty;
			return true;

		}

		public override bool Update(object obj, IUser user, out string err)
		{
			if (!(obj is JSubMessage model))
			{
				err = "系统错误";
				return false;
			}

			if (string.IsNullOrEmpty(model.Name))
			{
				err = "名称不能为空!";
				return false;
			}

			if (DB.FSql.Select<JSubMessage>().Where(a=> a.ID != model.ID && a.Name == model.Name)
				.Any())
			{
				err = "已有相同名称业务员不能修改!";
				return false;
			}

			DB.FSql.Update<JSubMessage>()
				.SetSource(model)
				.IgnoreColumns(nameof(JSubMessage.IsCheck), nameof(JSubMessage.CheckDate), nameof(JSubMessage.Checker))
				.ExecuteAffrows();

			err = null;
			return true;
		}

		public override bool Delete(object obj, IUser user, out string err)
		{
			if (!(obj is JSubMessage model))
			{
				err = "系统错误";
				return false;
			}

			DB.FSql.Update<JSubMessage>()
				.Where(a => a.ID == model.ID)
				.Set(a => a.Isdel, true)
				.ExecuteAffrows();

			err = null;
			return true;
		}

		public override bool Check(object obj, IUser user, out string err)
		{
			if (!(obj is JSubMessage model))
			{
				err = "系统错误";
				return false;
			}

			var time = DB.FSql.GetTime();

			DB.FSql.Update<JSubMessage>()
				.Where(a => a.ID == model.ID)
				.Set(a=> a.CheckDate,time )
				.Set(a=> a.IsCheck, true)
				.Set(a => a.Checker, user.Name)
				.ExecuteAffrows();

			err = null;
			return true;
		}

		public override bool UnCheck(object obj, IUser user, out string err)
		{
			if (!(obj is JSubMessage model))
			{
				err = "系统错误";
				return false;
			}

			var time = DB.FSql.GetTime();

			DB.FSql.Update<JSubMessage>()
				.Where(a => a.ID == model.ID)
				.Set(a => a.CheckDate, time)
				.Set(a => a.IsCheck, false)
				.Set(a => a.Checker, user.Name)
				.ExecuteAffrows();

			err = null;
			return true;
		}




		#endregion









	}
}



 
